Working of a capacitive touch sensor
We’re all accustomed to seeing capacitance in the form of leaded components or surface-mount packages, but actually, all we really need is two conductors separated by an insulating material.
The insulating separation between the touch-sensitive button and the surrounding copper creates a capacitor. In this case, the surrounding copper is connected to the ground node, and consequently, our touch-sensitive button can be modeled as a capacitor between the touch-sensitive signal and ground.
The effect of a finger
The finger is insulated from the capacitor by the PCB’s solder mask and usually also by a layer of plastic that separates the device’s electronics from the external environment. So the finger is not discharging the capacitor, and furthermore, the amount of charge stored in the capacitor at a particular moment is not the quantity of interest, rather, the quantity of interest is the capacitance at a particular moment.
- Finger as dielectric:
We certainly cannot change the physical dimensions of the capacitor just by touching it, but we can change the dielectric constant, because a human finger has different dielectric characteristics than the material (presumably air) that it is displacing. Since the human flesh is a good dielectric, the finger’s interaction with the capacitor’s electric field represents an increase in the dielectric constant and hence an increase in the capacitance.
- Finger as conductor:
We know that the human skin is also conductive. We can assume that this new capacitor created by the finger (let’s call it the finger cap) is in parallel with the existing PCB capacitor. This situation is a bit complex because the person using the touch-sensitive device is not electrically connected to the PCB’s ground node. We can think of the human body as providing a virtual ground because it has a relatively large capacity to absorb electric charge.
Code
Reading the touch sensor is straightforward. In the Arduino IDE, you use the touchRead() function, that accepts as argument, the GPIO you want to read.
void setup() {
Serial.begin(115200);
delay(1000); // gives time to bring up serial monitor
Serial.println("ESP32 Touch Test");
}
void loop() {
Serial.println(touchRead(4)); // gets value of Touch 0 pin = GPIO 4
delay(1000);
}
Use the touchRead() function, and pass as an argument as the pin you want to read from. In this case, the example uses T0, which is the touch sensor 0, in GPIO 4. You can either pass the touch sensor number (T0) or the GPIO number (4). We also can use the serial plotter to see the values.